package com.warm.common.excel;

import java.util.List;

/**
 * @Date: 2019/11/15 16:01
 * @Author: LChuang
 * @Description:
 */
public class TreeTool {

    public String[][] treeToTable(Tree tree){
        Integer level = tree.getLevel();
        Integer width = tree.getWidth();
        String[][] table = new String[level+1][width];
        List<Node> leafs = tree.getLeafs();
        // 先将最后一行设置到 table 中
        String[] names = new String[leafs.size()];
        String[] parentNames = new String[leafs.size()];
        for(int i = 0; i < leafs.size(); i++){
            names[i] = leafs.get(i).getName();
            parentNames[i] = leafs.get(i).getParentName();
        }

        genTable(tree, level, names, parentNames, table);

        return table;
    }

    /**
     * ******** 注意: 改变Node的内容会影响树 --> Node 相当与 Tree 的对象 这里直接使用二维数组存储值 不改变node的值
     * @return
     */
    private void genTable(Tree tree, Integer level, String[] names, String[] parentNames, String[][] table){

        for(int i = 0; i < names.length; i++){
            table[level][i] = names[i];
            /**
             * parent = null --> no change
             * parent != null --> parent.level != level --> name, parent no change
             *                    parent.level == level --> name = parentName, parentName = parentName.parent
             */

            //判断节点的level 和 实际的 level
            Node node = tree.getNode(parentNames[i], names[i]);
            if(parentNames != null && level > 0 && node.getLevel() == level){
                names[i] = parentNames[i];
                Node parentNode = tree.getParent(node);

                parentNames[i] = parentNode.getParentName();
            }
        }
        if(level > 0){
            genTable(tree, level-1, names, parentNames, table);
        }
    }

    /**
     * @param rowTree
     * @param colTree
     * @return
     */
    public String[][] assembleRowTable(Tree rowTree, Tree colTree){
        String[][] rowTable = this.treeToTable(rowTree);
        // 先获取列数相同节点的深度
        int colDepth = colTree.getLevel();
        // 根据 colDepth 设置行table, colDepth 为多少 --> 行table 第一列增加相同单元格
        int newWidth = rowTable[0].length + colDepth;
        String[][] table = new String[rowTable.length][newWidth];
        for(int i = 0; i < rowTable.length; i++){
            String[] row = new String[newWidth];
            for(int j = 0; j < newWidth; j++){
                if(j < colDepth){
                    row[j] = rowTable[0][0];
                }else {
                    row[j] = rowTable[i][j-colDepth];
                }
            }
            table[i] = row;
        }
        return table;
    }

    public String[][] combineTable(Tree rowTree, Tree colTree){
        String[][] rowTable = this.treeToTable(rowTree);
        String[][] colTable = this.treeToTable(colTree);
        int rowNum = rowTable.length + colTable[0].length;
        int colNum = rowTable[0].length + colTable.length -1;
        String[][] table = new String[rowNum][colNum];
        // rowTable first element --> copy colTable.length's
        for(int i = 0; i < rowNum; i++){
            String[] row = new String[colNum];
            for(int j = 0; j < colNum; j++){
                if(i < rowTable.length){
                    // set rowTable
                    if(j < colTable.length){
                        row[j] = rowTable[0][0];
                    }else {
                        row[j] = rowTable[i][j-colTable.length+1];
                    }
                }else {
                    // set colTable
                    if(j < colTable.length){
                        row[j] = colTable[j][i - rowTable.length];
                        //row[j] = colTable[j][colTable[0].length + rowTable.length - i - 1];
                    }else {
                        row[j] = "";
                    }
                }
            }
            table[i] = row;
        }

        return table;
    }

}
